<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Kong_Proc extends __Kong{
	
	function __construct() {

		parent::__construct();

		$this->load->module( "mysql" , $this->db );
		$this->load->module( "input" , $this->input );

	}
	function doit() {

		if ($this->input->post_exists("action")) {
			switch( $this->input->post("action") ) {
				case "new" :
					$this->newBlog();break;
//				case "modify" :
//					$this->_modify();break;
//				case "delete" :
//					$this->_delete();break;
			}
		}
	}
	
	function newBlog() {
		
		$this->load->module("files", $files);
		$this->load->model("admin/member", $member);
		
		$mem = $member->getMember();

		$category_idx	= $this->input->post("category_idx");
		$member_idx		= $mem['idx'];
		$writer			= $mem['nick'];
		$password		= $mem['password'];
		$subject		= $this->input->post("subject");
		$contents		= $this->input->post("contents");

		$files->upload();

		$dat = array(
			'board_id'		=> "blog",
			'category_idx'	=> $category_idx,
			'member_idx'	=> $member_idx,
			'writer'		=> $writer,
			'password'		=> $password,
			'subject'		=> $subject,
			'contents'		=> $contents,
			'files'			=> serialize( $files->getData() ),
			'ip'			=> $_SERVER['REMOTE_ADDR']
		);

		try {
			if ( $this->db->insert("wd_data", $dat) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {$e->report=0;echo $e;exit;}

		go($this->data->href."/".$this->data->kong_id);
	}
	
	function _modify() {

		$this->load->module("files", $this->files);
		$this->load->model("admin/member", $this->member);

		$idx = $this->input->post("idx");

		$member = $this->member->getMember();

		$dat = array(
			'id'		=> sha1($this->input->post("subject")),
			'type'		=> "blog",
			'member'	=> $member['idx'],
			'writer'	=> $member['nick'],
			'password'	=> $member['password'],
			'subject'	=> $this->input->post("subject"),
			'contents'	=> $this->input->post("contents")
		);

		if ($this->input->post_exists("category")) $dat['category'] = implode(",", $this->input->post("category"));
		else $dat['category'] = "";

		if ( $this->files->upload() ) $dat['files'] = serialize( $this->files->getData() );

		if ( $this->input->post_exists("thumb_source") ) $dat['option'] = serialize(array('thumb_source' => $this->input->post("thumb_source")));
		
		try {
			if ( $this->db->update("wd_data", $dat, array("idx" => $idx ) ) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {echo $e; exit;}
		
		go($this->data->kong_href."/".$idx);
	}
	function _delete() {

		try {
			if ( $this->db->delete("wd_data", array("idx" => $this->input->post("idx"))) === false ) throw new Error("DB를 쓰는 도중 실패하였습니다.");
		}
		catch(Error $e) {echo $e; exit;}
		
		go($this->data->kong_href);
		
	}

	
	
	// End
}